package com.dyh.quickdemozwei.dyhTp;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: yhdong
 * @Date: 2021/10/1 14:09
 */
public class DyhThreadPool  extends ThreadPoolExecutor {

    private final AtomicInteger submittedCount = new AtomicInteger(0);


    public DyhThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                         DyhQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }


    @Override
    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        else {
           this.submittedCount.incrementAndGet();
           try{
               super.execute(command);
           }
           catch (Throwable throwable){
               this.submittedCount.decrementAndGet();
               throw throwable;
           }
        }
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        submittedCount.decrementAndGet();
    }

    public AtomicInteger getSubmittedCount() {
        return submittedCount;
    }
}
